package com.yiwei.zxp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.IndexModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class HomeImageAdapter extends ItemAdapter<IndexModel.DatasBean.Purpose, HomeImageAdapter.DefaultHolder> {

    private Context context;

    public HomeImageAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_home_image, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        IndexModel.DatasBean.Purpose item = getItem(position);
        holder.txtName.setText(item.title);
        Glide.with(context).load(item.image).into(holder.imgIcon);
        holder.imgIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.title);
                }
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_name)
        TextView txtName;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String title);
    }
}
